package cn.xawl.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import cn.xawl.common.exception.BizCodeEnum;
import cn.xawl.gulimall.member.exception.InvalidPasswordException;
import cn.xawl.gulimall.member.exception.MemberNotExistException;
import cn.xawl.gulimall.member.exception.PhoneNumNotUniqueException;
import cn.xawl.gulimall.member.exception.UsernameNotUniqueException;
import cn.xawl.gulimall.member.vo.MemberLoginVo;
import cn.xawl.gulimall.member.vo.MemberVo;
import cn.xawl.gulimall.member.vo.WeiboMemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.xawl.gulimall.member.entity.MemberEntity;
import cn.xawl.gulimall.member.service.MemberService;
import cn.xawl.common.utils.PageUtils;
import cn.xawl.common.utils.R;


/**
 * 会员
 *
 * @author lh
 * @email lhm49869999@163.com
 * @date 2020-06-17 19:38:23
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/register")
    public R register(@RequestBody MemberVo member) {
        try {
            memberService.registerMember(member);
        } catch (UsernameNotUniqueException e) {
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), e.getMessage());
        } catch (PhoneNumNotUniqueException e) {
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), e.getMessage());
        }
        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLogin) {

        try {
            MemberEntity memberEntity = memberService.memberLogin(memberLogin);
            return R.ok().setData(memberEntity);
        } catch (MemberNotExistException e) {
            return R.error(BizCodeEnum.USER_NOT_EXIST_EXCEPTION.getCode(), e.getMessage());
        } catch (InvalidPasswordException e) {
            return R.error(BizCodeEnum.INVALID_PASSWORD_EXCEPTION.getCode(), e.getMessage());
        }

    }

    @PostMapping("/weibo/login")
    public R login(@RequestBody WeiboMemberVo weiboMember) {

        MemberEntity member = memberService.weiboMemberLoginOrRegister(weiboMember);
        if (member != null) {
            return R.ok().put("data", member);
        }
        return R.error();
    }

}
